package com.twu.biblioteca.app;

import com.twu.biblioteca.app.book.Book;
import com.twu.biblioteca.app.book.CheckoutDetails;
import com.twu.biblioteca.app.book.CheckoutStatus;
import com.twu.biblioteca.app.movie.Movie;
import com.twu.biblioteca.app.user.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Library {
    private List<Book> books;
    private List<Movie> movies;
    private List<User> userRecords;

    private HashMap<Book, CheckoutDetails> booksWithCheckoutDetails = new HashMap<Book, CheckoutDetails>();
    private HashMap<Movie, CheckoutDetails> moviesWithCheckoutDetails = new HashMap<Movie, CheckoutDetails>();


    public Library(List<Book> books, List<Movie> movies, List<User> userRecords) {
        this.books = books;
        this.movies = movies;
        this.userRecords = userRecords;

        setDefaultCheckoutDetails(books, movies);
    }

    private void setDefaultCheckoutDetails(List<Book> books, List<Movie> movies) {
        if (books != null) {
            for (Book book : books) {
                booksWithCheckoutDetails.put(book, new CheckoutDetails(null, CheckoutStatus.AVAILABLE));
            }
        }

        if (movies != null) {
            for (Movie movie : movies) {
                moviesWithCheckoutDetails.put(movie, new CheckoutDetails(null, CheckoutStatus.AVAILABLE));
            }
        }
    }

    public List<Book> getAllBooks() {
        return this.books;
    }

    public List<Book> getAvailableBooks() {
        List<Book> availableBooks = new ArrayList<Book>();
        for (Book book : this.books) {
            if (book.getCheckoutDetails().getCheckoutStatus() == CheckoutStatus.AVAILABLE) {
                availableBooks.add(book);
            }
        }
        return availableBooks;
    }

    public List<Movie> getAvailableMovies() {
        List<Movie> availableMovies = new ArrayList<Movie>();
        for (Movie movie : this.movies) {
            if (movie.getCheckoutDetails().getCheckoutStatus() == CheckoutStatus.AVAILABLE) {
                availableMovies.add(movie);
            }
        }
        return availableMovies;
    }

    public boolean checkoutBook(Book selectedBook, User user) {
        return checkoutItem(books, selectedBook, user);
    }

    public boolean checkoutMovie(Movie selectedMovie, User user) {
        return checkoutItem(movies, selectedMovie, user);
    }

    public boolean checkoutItem(List<? extends Item> items, Item selectedItem, User user) {
        if (items.indexOf(selectedItem) != -1) {
            items.get(items.indexOf(selectedItem)).setCheckoutDetails(CheckoutStatus.CHECKED_OUT, user);
            return true;
        } else {
            return false;
        }
    }

    public boolean returnBook(String bookTitle) {
        for (Book book : books) {
            if (book.getTitle().equals(bookTitle)) {
                book.setCheckoutDetails(CheckoutStatus.AVAILABLE, null);
                return true;
            }
        }
        return false;
    }


    public User getUserFromLoginDetails(String libraryNumber, String password) {
        for (User user : userRecords) {
            if (user.getLibraryNumber().equals(libraryNumber) && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }


    public CheckoutDetails getCheckoutDetails(Book book) {
        return booksWithCheckoutDetails.get(book);
    }

    public CheckoutDetails getCheckoutDetails(Movie movie) {
        return moviesWithCheckoutDetails.get(movie);
    }
}
